home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0997.zip / stereo.txt < prev    next >
Text File  |  1997-07-22  |  4KB  |  136 lines

  1. _Stereoscopic Imaging_
  2. by Andy Ramm
  3.  
  4. Example 1: 
  5.  
  6. (a)
  7. window (-top * aspect + (half_sep * clip_near / distance), top * aspect + (half_sep * clip_near / distance), -top, top, clip_near, clip_far); translate (half_sep, 0.0, 0.0);
  8.  
  9. (b)
  10. window (-top * aspect - (half_sep * clip_near / distance), top * aspect - (half_sep * clip_near / distance), -top, top, clip_near, clip_far); translate (-half_sep, 0.0, 0.0);
  11.  
  12.  
  13. Listing One
  14. #if STEREO
  15. #define EYE_OFFSET 0.200    // default viewpoint separation
  16. #define EYE_ADJUST -0.070   // default horizontal image shift adjustment
  17.  
  18. int winWidth, winHeight;    // client window
  19. int winAdjust=30;       // Y height adjustment to "above" viewport 
  20.  
  21. GLfloat eyeDist=4.5;        // Z distance for zero parallax setting
  22. GLfloat eyeOffset=EYE_OFFSET;   // X offset for left/right viewpoint separation
  23. GLfloat eyeAdjust=EYE_ADJUST;   // X adjustment for horizontal image shift
  24. #endif
  25.  
  26. static void CALLBACK Reshape(int width, int height)
  27. {
  28. #if STEREO
  29.     winWidth = width;
  30.     winHeight = height;
  31.  
  32.     // viewport and perspective matrixes must be updated every drawn frame to 
  33.     // render left/right views in above/below format onto same client window
  34. #else
  35.     glViewport(0, 0, (GLint)width, (GLint)height);
  36.     
  37.     glMatrixMode(GL_PROJECTION);
  38.     glLoadIdentity();
  39.     glFrustum(-1, 1, -1, 1, 1, 10);
  40.     gluLookAt(2, 2, 2, 0, 0, 0, 0, 0, 1);
  41.     glMatrixMode(GL_MODELVIEW);
  42. #endif
  43. }
  44. static void CALLBACK Draw(void)
  45. {
  46. #if STEREO
  47.     // calculate delta-X translation for left or right perspective view
  48.     GLfloat dx0 = eyeAdjust;    // asymmetrical viewing pyramid offset
  49.     GLfloat dx1 = eyeOffset;    // viewpoint separation
  50.  
  51.     // set "above" viewport for left eye rendering
  52.     glViewport(0, (GLint)(winHeight/2 + winAdjust), (GLint)winWidth, 
  53.                                            (GLint)(winHeight/2 - winAdjust));
  54.     // set left-eye perspective 
  55.     glMatrixMode(GL_PROJECTION);
  56.     glLoadIdentity();
  57.     glFrustum(-1-dx0, 1-dx0, -1, 1, 1, 10); // skew viewer symmetry leftward
  58.     glTranslatef(0+dx1, 0, 0);          // move rightward for left eye view
  59.     glTranslatef(0, 0, -3);             // pull back to look at
  60.     glMatrixMode(GL_MODELVIEW);
  61. #endif // STEREO
  62.  
  63.     glLoadIdentity();
  64.     glRotatef(xRotation, 1, 0, 0);
  65.     glRotatef(yRotation, 0, 1, 0);
  66.     glRotatef(zRotation, 0, 0, 1);
  67.  
  68.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  69.  
  70.     glColor3f(1.0, 1.0, 1.0);
  71.     switch (whichQuadric) {
  72.       case 0:
  73.     glTranslatef(0, 0, -height/20.0);
  74.     gluCylinder(quadObj,radius1/10.0,radius2/10.0,height/10.0,slices,stacks);
  75.     break;
  76.       case 1:
  77.     gluSphere(quadObj, radius1/10.0, slices, stacks);
  78.     break;
  79.       case 2:
  80.     gluPartialDisk(quadObj, radius2/10.0, radius1/10.0, slices, 
  81.                stacks, angle1, angle2);
  82.     break;
  83.       case 3:
  84.     gluDisk(quadObj, radius2/10.0, radius1/10.0, slices, stacks);
  85.     break;
  86.     }
  87. # if STEREO
  88.     // set "below" viewport for right eye rendering
  89.     glViewport(0, 0, (GLint)winWidth, (GLint)(winHeight/2 - winAdjust));
  90.  
  91.     // set right-eye perspective
  92.     glMatrixMode(GL_PROJECTION);
  93.     glLoadIdentity();
  94.     glFrustum(-1+dx0, 1+dx0, -1, 1, 1, 10); // skew viewer symmetry rightward
  95.     glTranslatef(0-dx1, 0, 0);          // move leftward for right eye view
  96.     glTranslatef(0, 0, -3);             // pull back to look at
  97.     glMatrixMode(GL_MODELVIEW);
  98.  
  99.     // render scene again for right-eye view
  100.     glLoadIdentity();
  101.     glRotatef(xRotation, 1, 0, 0);
  102.     glRotatef(yRotation, 0, 1, 0);
  103.     glRotatef(zRotation, 0, 0, 1);
  104.  
  105.     glColor3f(1.0, 1.0, 1.0);
  106.     switch (whichQuadric) {
  107.       case 0:
  108.     glTranslatef(0, 0, -height/20.0);
  109.     gluCylinder(quadObj, radius1/10.0, radius2/10.0, height/10.0, 
  110.             slices, stacks);
  111.     break;
  112.       case 1:
  113.     gluSphere(quadObj, radius1/10.0, slices, stacks);
  114.     break;
  115.       case 2:
  116.     
  117.     gluPartialDisk(quadObj, radius2/10.0, radius1/10.0, slices, 
  118.                stacks, angle1, angle2);
  119.     break;
  120.       case 3:
  121.     gluDisk(quadObj, radius2/10.0, radius1/10.0, slices, stacks);
  122.     break;
  123.     }
  124. #endif // STEREO
  125.     glFlush();
  126.  
  127.     if (doubleBuffer) {
  128.     auxSwapBuffers();
  129.     }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.